package com.codedancer.campus.manager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.codedancer.campus.common.exception.BusinessException;
import com.codedancer.campus.common.utils.UUIDGenerator;
import com.codedancer.campus.manager.service.ICommentReportTypeService;
import com.codedancer.campus.manager.service.bo.commentReport.CommentReportTypeAddOrUpdateBo;
import com.codedancer.campus.manager.service.bo.commentReport.CommentReportTypeSearchBo;
import com.codedancer.campus.manager.service.vo.commentReport.CommentReportTypeVo;
import com.codedancer.campus.mapper.CommentReportMapper;
import com.codedancer.campus.mapper.CommentReportTypeMapper;
import com.codedancer.campus.po.CommentReportType;
import com.codedancer.campus.qo.commentReport.CommentReportTypeSearchQo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class CommentReportTypeServiceImpl implements ICommentReportTypeService {

  @Autowired
  private CommentReportTypeMapper commentReportTypeMapper;
  @Autowired
  private CommentReportMapper commentReportMapper;

  /**
   * 添加评论举报类型
   *
   * @param commentReportTypeAddOrUpdateBo
   */
  @Override
  public void add(CommentReportTypeAddOrUpdateBo commentReportTypeAddOrUpdateBo) {
    // 判断该名称是否存在
    if (commentReportTypeMapper.existsByColumn("name", commentReportTypeAddOrUpdateBo.getName())) {
      throw new BusinessException("评论举报类型名称已存在，请重新填写");
    }
    CommentReportType commentReportType = BeanUtil.copyProperties(commentReportTypeAddOrUpdateBo,
        CommentReportType.class);
    commentReportType.setUuid(UUIDGenerator.getUUID());
    commentReportTypeMapper.insert(commentReportType);
  }

  /**
   * 修改评论举报类型
   *
   * @param commentReportTypeAddOrUpdateBo
   */
  @Override
  public void update(CommentReportTypeAddOrUpdateBo commentReportTypeAddOrUpdateBo) {
    CommentReportType commentReportType = commentReportTypeMapper.findById(
            commentReportTypeAddOrUpdateBo.getUuid())
        .orElseThrow(() -> new BusinessException("评论举报类型不存在"));
    // 判断是否修改了名称
    if (commentReportType.getName().equals(commentReportTypeAddOrUpdateBo.getName())) {
      throw new BusinessException("评论举报类型名称未做任何修改");
    }
    // 判断名称是否存在
    if (commentReportTypeMapper.existsByColumn("name", commentReportTypeAddOrUpdateBo.getName())) {
      throw new BusinessException("评论举报类型名称已存在，请重新填写");
    }
    commentReportType.setName(commentReportTypeAddOrUpdateBo.getName());
    commentReportTypeMapper.updateById(commentReportType);
  }

  /**
   * 获取全部评论举报类型
   *
   * @return
   */
  @Override
  public List<CommentReportTypeVo> list(CommentReportTypeSearchBo commentReportTypeSearchBo) {
    List<CommentReportType> commentReportTypes = commentReportTypeMapper.list(
        BeanUtil.copyProperties(commentReportTypeSearchBo,
            CommentReportTypeSearchQo.class));
    return BeanUtil.copyToList(commentReportTypes, CommentReportTypeVo.class);
  }

  /**
   * 删除评论举报类型
   *
   * @param uuid
   */
  @Override
  public void delete(String uuid) {
    if (!commentReportTypeMapper.existsByUuId(uuid)) {
      throw new BusinessException("评论举报类型不存在");
    }
    // 是否绑定了评论举报
    if (commentReportMapper.existsByCommentReportTypeUuid(uuid)) {
      throw new BusinessException("评论举报类型已被使用，不能删除");
    }
    commentReportTypeMapper.deleteById(uuid);
  }
}
